 /* Wait for BSS powerup */
if (SOC_waitBSSPowerUp(socHandle, &errCode) < 0)
{
    /* Debug Message: */
    System_printf ("Debug: SOC_waitBSSPowerUp failed with Error [%d]\n", errCode);
    return 0;
}

Mailbox_init(MAILBOX_TYPE_MSS);



MMWave_InitCfg      initCfg;
initCfg.domain                  = MMWave_Domain_MSS;
initCfg.socHandle               = MMWave_Global_Params.handle.socHandle;
initCfg.eventFxn                = MMWave_eventFxnCallback;  //事件回调
initCfg.linkCRCCfg.useCRCDriver = 0U;           //1开启CRC 0关闭CRC
initCfg.linkCRCCfg.crcChannel   = CRC_Channel_CH1;      //CRC通道1
initCfg.cfgMode                 = MMWave_ConfigurationMode_FULL;  //全部模式 也可以设置成最小全部模式
initCfg.executionMode           = MMWave_ExecutionMode_ISOLATION;  //MSS和DSS不通讯 如果需要 则换个模式

//以下全是回调
initCfg.cooperativeModeCfg.cfgFxn   = MMWave_cfgFxnCallback;
initCfg.cooperativeModeCfg.closeFxn = MMWave_closeFxnCallback;
initCfg.cooperativeModeCfg.openFxn  = MMWave_openFxnCallback;
initCfg.cooperativeModeCfg.startFxn = MMWave_startFxnCallback;
initCfg.cooperativeModeCfg.stopFxn  = MMWave_stopFxnCallback;

/* Initialize and setup the MMWave Control module */
MMWave_Global_Params.handle.MMWave_handle = MMWave_init (&initCfg, &errCode);
if (MMWave_Global_Params.handle.MMWave_handle == NULL)
{
    /* Error: Unable to initialize the MMWave control module */
    MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
    System_printf ("Error: MMWave Control Initialization failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
    return;
}

/* Synchronization: This will synchronize the execution of the control module
     * between the domains. This is a prerequiste and always needs to be invoked. */
    //同步
    if (MMWave_sync (MMWave_Global_Params.handle.MMWave_handle, &errCode) < 0)
    {
        /* Error: Unable to synchronize the MMWave control module */
        MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
        System_printf ("Error: MMWave Control Synchronization failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
        return;
    }
/*****************************************************************************
    * Launch the MMWave control execution task
    * - This should have a higher priroity than any other task which uses the
    *   MMWave control API
    *****************************************************************************/
    //这里是开了一个线程循环调用
    Task_Params_init(&taskParams);
    taskParams.priority  = 5;
    taskParams.stackSize = 3*1024;
    MMWave_Global_Params.task.MMWaveCtrl = Task_create(Ctrl_MMWave, &taskParams, NULL);
void Ctrl_MMWave(UArg arg0, UArg arg1)
{
    int32_t errCode;
    MMWave_ErrorLevel   errorLevel;
    int16_t             mmWaveErrorCode;
    int16_t             subsysErrorCode;

    while (1)
    {
        /* Execute the MMWave control module: */
        if (MMWave_execute (MMWave_Global_Params.handle.MMWave_handle, &errCode) < 0)
        {
            MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
            System_printf ("Error: MMWave control execution failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
        }
    }
}

MMWave_OpenCfg      openCfg;

openCfg.freqLimitLow  = 600U;   //低频限制
openCfg.freqLimitHigh = 640U;   //高频限制

openCfg.chCfg.rxChannelEn = 0x000F;  //开启四个RX
openCfg.chCfg.txChannelEn = 0x0007;  //开启四个TX
openCfg.chCfg.cascading   = 0x0000;  //不开启级联
openCfg.chCfg.cascadingPinoutCfg = 0;  //直接给0

openCfg.lowPowerMode.lpAdcMode = 0x0000; //Regular ADC mode

openCfg.adcOutCfg.fmt.b2AdcBits = 2; //16bit
openCfg.adcOutCfg.fmt.b8FullScaleReducFctr = 0; //16bit只能为0
openCfg.adcOutCfg.fmt.b2AdcOutFmt = 2;  //Complex with Image band

openCfg.defaultAsyncEventHandler    = MMWave_DefaultAsyncEventHandler_MSS;

openCfg.disableFrameStartAsyncEvent = false;
openCfg.disableFrameStopAsyncEvent  = false;

openCfg.useCustomCalibration        = false;  // 无自定义校准 false表示默认启用所有校准
openCfg.customCalibrationEnableMask = 0x0;

openCfg.calibMonTimeUnit            = 1;   //一帧一次校准

/*
MMWave_CalibrationData      calibrationData;
memset ((void*)&calibrationCfg, 0 , sizeof(MMWave_CalibrationCfg));

calibrationData.ptrCalibData->calibChunk
calibrationData.ptrPhaseShiftCalibData->PhShiftcalibChunk
*/

/* Open the mmWave module: */
if (MMWave_open (MMWave_Global_Params.handle.MMWave_handle, &openCfg, NULL, &errCode) < 0)
{
    /* Error: decode and Report the error */
    MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
    System_printf ("Error: mmWave Open failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
}
MMWave_CtrlCfg      ctrlCfg;

ctrlCfg.dfeDataOutputMode = MMWave_DFEDataOutputMode_FRAME;

ctrlCfg.u.frameCfg.profileHandle[0]=MMWave_Global_Params.handle.profileHandle[0];
ctrlCfg.u.frameCfg.profileHandle[1]=NULL;
ctrlCfg.u.frameCfg.profileHandle[2]=NULL;
ctrlCfg.u.frameCfg.profileHandle[3]=NULL;

ctrlCfg.u.frameCfg.frameCfg.chirpStartIdx = 0;
ctrlCfg.u.frameCfg.frameCfg.chirpEndIdx = 2;
ctrlCfg.u.frameCfg.frameCfg.numLoops = 96;
ctrlCfg.u.frameCfg.frameCfg.numFrames = 0;
ctrlCfg.u.frameCfg.frameCfg.numAdcSamples = 96;
ctrlCfg.u.frameCfg.frameCfg.framePeriodicity = 11000000;  //55ms
ctrlCfg.u.frameCfg.frameCfg.triggerSelect = 0x0001; //软件API触发

ctrlCfg.u.frameCfg.frameCfg.numDummyChirpsAtEnd = 0;
ctrlCfg.u.frameCfg.frameCfg.frameTriggerDelay = 0;

/*
//frame模式不用配
ctrlCfg.u.continuousModeCfg.cfg.startFreqConst
ctrlCfg.u.continuousModeCfg.cfg.txOutPowerBackoffCode
ctrlCfg.u.continuousModeCfg.cfg.txPhaseShifter
ctrlCfg.u.continuousModeCfg.cfg.digOutSampleRate
ctrlCfg.u.continuousModeCfg.cfg.hpfCornerFreq1
ctrlCfg.u.continuousModeCfg.cfg.hpfCornerFreq2
ctrlCfg.u.continuousModeCfg.cfg.rxGain
ctrlCfg.u.continuousModeCfg.cfg.vcoSelect
ctrlCfg.u.continuousModeCfg.dataTransSize
*/
//ctrlCfg.u.advancedFrameCfg.profileHandle = //NULL;  //frame模式不用配

/* Configure the mmWave module: */
if (MMWave_config (MMWave_Global_Params.handle.MMWave_handle, &ctrlCfg, &errCode) < 0)
{
    /* Error: Report the error */
    MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
    System_printf ("Error: mmWave Config failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
}
rlProfileCfg_t  profileCfg;
rlChirpCfg_t    chirpCfg;
memset ((void*)&profileCfg, 0 , sizeof(rlProfileCfg_t));
memset ((void*)&chirpCfg, 0 , sizeof(rlChirpCfg_t));

profileCfg.profileId = 0;
profileCfg.pfVcoSelect = 0x02;  //内置 VCO2 选择60-64GHz
profileCfg.pfCalLutUpdate = 0x00;  //校准
profileCfg.startFreqConst = 0x58E3A1CD;  //60GHz
profileCfg.idleTimeConst = 0x2710;  //100us
profileCfg.adcStartTimeConst = 0x258;  //6us
profileCfg.rampEndTime = 0x1770;  //60us
profileCfg.txOutPowerBackoffCode = 0;  //全为0
profileCfg.txPhaseShifter = 0;  //全为0
profileCfg.freqSlopeConst = 0x05E7;  //54.713 MHz/us
profileCfg.txStartTime = 0;  //0us
profileCfg.numAdcSamples = 96;  //96
profileCfg.digOutSampleRate = 0x0B86;  //2950ksps
profileCfg.hpfCornerFreq1 = 0;  //175K
profileCfg.hpfCornerFreq2 = 0;  //350K
profileCfg.txCalibEnCfg = 0xFFFF;  //不适用于IWR6843 ES 1.0
profileCfg.rxGain = 0x68; //40dB/34dB

MMWave_Global_Params.handle.profileHandle[0] = MMWave_addProfile(MMWave_Global_Params.handle.MMWave_handle,&profileCfg,&errCode);
if(MMWave_Global_Params.handle.profileHandle[0] == NULL)
{
    MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
    System_printf ("Error: mmWave profileCfg failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
}

chirpCfg.chirpStartIdx=0;
chirpCfg.chirpEndIdx=0;
chirpCfg.profileId=0;
chirpCfg.startFreqVar=0;
chirpCfg.freqSlopeVar=0;
chirpCfg.idleTimeVar=0;
chirpCfg.adcStartTimeVar=0;
chirpCfg.txEnable=(1<<0);

//TDM模式 用for循环配置三次 每次的chirpStartIdx chirpEndIdx均不同 分别为00 11 22 另外 打开的TX也不同 分别为TX0 TX1 TX2
//这三个chirp配置都属于同一个profileHandle下
for(i=0;i<3;i++)
{
    chirpCfg.chirpStartIdx=i;
    chirpCfg.chirpEndIdx=i;
    chirpCfg.txEnable=(1<<i);
    MMWave_Global_Params.handle.chirpHandle[i] = MMWave_addChirp(MMWave_Global_Params.handle.profileHandle[0],&chirpCfg,&errCode);
    if(MMWave_Global_Params.handle.chirpHandle[i] == NULL)
    {
        MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
        System_printf ("Error: mmWave chirpCfg %d failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
    }
}

//然后下面再是MMWave_config 函数和配置
MMWave_CalibrationCfg       calibrationCfg;
memset ((void *)&calibrationCfg, 0, sizeof(MMWave_CalibrationCfg));

calibrationCfg.dfeDataOutputMode = MMWave_DFEDataOutputMode_FRAME;

calibrationCfg.u.chirpCalibrationCfg.enableCalibration = true;
calibrationCfg.u.chirpCalibrationCfg.enablePeriodicity = true;
calibrationCfg.u.chirpCalibrationCfg.periodicTimeInFrames = 10;  //每10帧一次校准

//calibrationCfg.u.contCalibrationCfg.enableOneShotCalibration  //frame模式不用配

/* Start the mmWave module: The configuration has been applied successfully. */
if (MMWave_start(MMWave_Global_Params.handle.MMWave_handle, &calibrationCfg, &errCode) < 0)
{
    /* Error/Warning: Unable to start the mmWave module */
    MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
    System_printf ("Error: mmWave Start failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
    /* datapath has already been moved to start state; so either we initiate a cleanup of start sequence or
       assert here and re-start from the beginning. For now, choosing the latter path */
}
int32_t         errCode;
MMWave_ErrorLevel   errorLevel;
int16_t             mmWaveErrorCode;
int16_t             subsysErrorCode;
if (MMWave_start(MMWave_Global_Params.handle.MMWave_handle, &calibrationCfg, &errCode) < 0)
{
    /* Error/Warning: Unable to start the mmWave module */
    MMWave_decodeError (errCode, &errorLevel, &mmWaveErrorCode, &subsysErrorCode);
    System_printf ("Error: mmWave Start failed [Error Level: %d Error code: %d Subsystem: %d]\n",errorLevel,mmWaveErrorCode, subsysErrorCode);
    /* datapath has already been moved to start state; so either we initiate a cleanup of start sequence or
       assert here and re-start from the beginning. For now, choosing the latter path */
}
